import 'dart:async';

import 'package:flutter/material.dart';
// import 'package:amap_flutter_location/amap_flutter_location.dart';
// import 'package:amap_flutter_location/amap_location_option.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:permission_handler/permission_handler.dart';

class GaodeLocation extends StatefulWidget {
  const GaodeLocation({super.key});

  @override
  _GaodeLocationState createState() => _GaodeLocationState();
}

class _GaodeLocationState extends State<GaodeLocation> {
  // late AMapFlutterLocation _locationPlugin;

  //监听定位
  late StreamSubscription<Map<String, Object>> _locationListener;
  String _mResult = "";

  //获取数据
  // Map<String, Object> _locationResult;
  String _latitude = ""; //纬度
  String _longitude = ""; //经度
  @override
  void initState() {
    super.initState();
    requestPermissions();
    // _locationPlugin = AMapFlutterLocation();
    // _locationPlugin.setLocationOption(AMapLocationOption());
    // _locationListener = _locationPlugin.onLocationChanged().listen((Map<String, Object> result) {
    //   setState(() {
    //     print(result);
    //     print("-----");
    //     print(result is Map);
    //     // _locationResult = result;
    //     _latitude = result["latitude"].toString();
    //     _longitude = result["longitude"].toString();
    //     print("_latitude  $_latitude  _longitude $_longitude");
    //     _mResult = result.toString();
    //   });
    // });
    // permission();
  }

  Future<void> permission() async {
    if (await Permission.location.isRestricted) {
      // _locationPlugin.startLocation();
    } else {
      await Permission.location.onDeniedCallback(() {
        // Your code
        print("onDeniedCallback");
      }).onGrantedCallback(() {
        print("onGrantedCallback");
        // _locationPlugin.startLocation();

        // Your code
      }).onPermanentlyDeniedCallback(() {
        print("onPermanentlyDeniedCallback");
        // Your code
      }).onRestrictedCallback(() {
        print("onRestrictedCallback");
      }).onLimitedCallback(() {
        print("onLimitedCallback");
        // Your code
      }).onProvisionalCallback(() {
        print("onProvisionalCallback");
        // Your code
      }).request();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: InkWell(
          child: Text("高德定位"),
          onTap: () {
            // AMapFlutterLocation.setApiKey("ef14770dc8b1388639f56b7350cc3bd7", "ios ApiKey");
            // AMapFlutterLocation.updatePrivacyShow(true, true);
            // AMapFlutterLocation.updatePrivacyAgree(true);
            // Fluttertoast.showToast(msg: "点击了隐私协议 确定");
            // _locationPlugin.startLocation();
          },
        ),
      ),
      body: Column(
        children: [
          Text("纬度：$_latitude"),
          Text("经度：$_longitude"),
          Text("结果：$_mResult"),
        ],
      ),
    );
  }

  Future<void> requestPermissions() async {
    final status = await Permission.location.request();
    if (status.isGranted) {
      // AMapFlutterLocation.setApiKey("ef14770dc8b1388639f56b7350cc3bd7", "ios ApiKey");
      // AMapFlutterLocation.updatePrivacyShow(true, true);
      // AMapFlutterLocation.updatePrivacyAgree(true);
      // _locationPlugin = AMapFlutterLocation();
      // _locationPlugin.setLocationOption(AMapLocationOption());
      // // 注册定位结果监听
      // _locationListener = _locationPlugin.onLocationChanged().listen((Map<String, Object> result) {
      //   setState(() {
      //     print(result);
      //     print("-----");
      //     print(result is Map);
      //     _latitude = result["latitude"].toString();
      //     _longitude = result["longitude"].toString();
      //     print("_latitude  $_latitude  _longitude $_longitude");
      //     _mResult = result.toString();
      //   });
      // });
      // _locationPlugin.startLocation();
    } else {
      Fluttertoast.showToast(msg: "请授予位置权限");
    }
  }

  @override
  void dispose() {
    super.dispose();
    _locationListener.cancel();
    // _locationPlugin.destroy();
  }
}
